package com.gamex.findit.controller.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gamex.findit.model.Notice;
import com.gamex.findit.service.NoticeService;

@Controller("adminNotice")
public class NoticeController {
	
	private static final Logger logger = LoggerFactory.getLogger(NoticeController.class);
	
	@Autowired NoticeService noticeService;
	
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param notice
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/noticeList")
	public String noticeList( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, Model model
							, @ModelAttribute Notice notice) throws Exception{

		int count = noticeService.getNoticeCount(notice);
		List<Notice> result = noticeService.getNotice(notice);
		
		model.addAttribute("list", result);
		model.addAttribute("count", count);
		model.addAttribute("pageNo", notice.getPageNo());
		model.addAttribute("pageGroup", notice.getPageGroup());
		model.addAttribute("searchParam", notice);
	return "/notice/notice";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param notice
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/viewNotice.*")
	public String viewNotice( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, ModelMap model
							, @ModelAttribute Notice notice) throws Exception{
		
		model.addAttribute("view", noticeService.getNoticeView(notice.getnId()));
		model.addAttribute("searchParam", notice);
	return "/notice/viewnotice";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param notice
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/addNotice.*")
	public String addNotice( HttpSession session
						   , HttpServletRequest request
						   , HttpServletResponse response
						   , Model model
						   , @ModelAttribute Notice notice) throws Exception{
		
		noticeService.insertNotice(notice);
		
	return "redirect:/admin/noticeList.json";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param notice
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/updateNotice.*")
	public String updateNotice( HttpSession session
							  , HttpServletRequest request
							  , HttpServletResponse response
							  , Model model
							  , @ModelAttribute Notice notice) throws Exception{
		
		noticeService.updateNotice(notice);
		
	return "redirect:/admin/noticeList.json";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param notice
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/deleteNotice.*")
	public String deleteNotice( HttpSession session
							  , HttpServletRequest request
							  , HttpServletResponse response
							  , Model model
							  , @ModelAttribute Notice notice) throws Exception{
		
		
		noticeService.deleteNotice(notice.getnId());
		
	return "redirect:/admin/noticeList.json";
	}
	
}
